package com.jshongli.myapplication.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


import com.jshongli.myapplication.MyApplication;
import com.jshongli.myapplication.R;
import com.jshongli.myapplication.common.utils.SoftInputUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2017/7/13.
 */

public abstract class BaseFragment extends Fragment {

    protected String TAG = BaseFragment.class.getSimpleName();
    // 标示是否第一次初始化数据
    protected boolean mIsFirstInitData = true;

    protected Context mContext;
    protected Activity mActivity;
    protected MyApplication mApplication;

    private LinearLayout mRootView;
    protected FrameLayout mViewContent;

    private Unbinder mUnbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRootView == null) {
            this.mContext = getActivity();
            this.mActivity = getActivity();
            mRootView = (LinearLayout) inflater.inflate(R.layout.fragment_base, container, false);
            SoftInputUtils.setupUI(mRootView, mContext);

            if (mViewContent == null) {
                mViewContent = (FrameLayout) mRootView.findViewById(R.id.view_content);
                setRealContentView();
            }

        } else {
            if (mRootView.getParent() != null) {
                // 把当前Root从其父控件中移除
                ((ViewGroup) mRootView.getParent()).removeView(mRootView);
            }
        }

        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mIsFirstInitData) {
            mIsFirstInitData = false;
            this.mApplication = (MyApplication) getActivity().getApplication();

            initData();
            initView();
        }
    }

    private void setRealContentView() {

        View realContentView = LayoutInflater.from(mContext).inflate(getLayoutId(), mViewContent, true);
        mUnbinder = ButterKnife.bind(this, realContentView);

    }


    protected abstract int getLayoutId();

    protected void initData() {
    }

    protected void initView() {
    }

    public boolean openActivity(Context context, Class<?> target, Bundle paramter, boolean isFinish) {
        boolean flag = true;
        try {
            Intent itent = new Intent(context, target);
            if (paramter != null) {
                itent.putExtras(paramter);
            }
            this.startActivity(itent);
            mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            if (isFinish) {
                mActivity.finish();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            flag = false;
        }
        return flag;
    }

    public boolean openActivity(Context mContext, Class<?> target, boolean isFinish) {
        return openActivity(mContext, target, null, isFinish);
    }

    protected boolean openActivityForResult(Class<?> target, int requestCode) {
        return openActivityForResult(target, null, requestCode);
    }

    protected boolean openActivityForResult(Class<?> target, Bundle paramter, int requestCode) {
        boolean flag = true;
        try {
            Intent itent = new Intent(mActivity, target);
            if (paramter != null) {
                itent.putExtras(paramter);
            }
            mActivity.startActivityForResult(itent, requestCode);
            mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception ex) {
            ex.printStackTrace();
            flag = false;
        }
        return flag;
    }

    protected void hideKeyboard() {
        InputMethodManager inputManager = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null != inputManager) {
            boolean isOpen = inputManager.isActive(); // isOpen若返回true，则表示输入法打开
            if (isOpen) {
                if (mActivity.getCurrentFocus() != null) { // 强制关闭软键盘
                    inputManager.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
